package com.like.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.like.entity.ImageReq;
import com.like.entity.M;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.InfoService;
import com.like.network.RetrofitUtil;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class ImageUtils {

    public static InfoService mInfoService = RetrofitUtil.getNoAesService(InfoService.class);

    public static void showImage(final Context context, String res, String token, final ImageView img) {
        ImageReq req = null;
        if (TextUtils.isEmpty(res) || TextUtils.equals("null", res.toLowerCase()))
            req = new ImageReq("0");
        else
            req = new ImageReq(res);
        Call<ResponseBody> call = mInfoService.getImage(Constants.getGUID(), token, req);
        call.enqueue(new RetrofitCallback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (!response.isSuccessful())
                    return;
                ResponseBody body = response.body();
                try {
                    Glide.with(context.getApplicationContext()).load(body.bytes()).dontAnimate().into(img);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

}
